package lanou.a36ke_01.fragmengs.discoveryfragments;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

import lanou.a36ke_01.R;
import lanou.a36ke_01.activity.LogonActivity;
import lanou.a36ke_01.activity.discoveryactivity.AcademyActivity;
import lanou.a36ke_01.activity.discoveryactivity.BuyerNoticeActivity;
import lanou.a36ke_01.activity.discoveryactivity.DiscoveryActivity;
import lanou.a36ke_01.activity.discoveryactivity.EntrePreneursActivity;
import lanou.a36ke_01.activity.discoveryactivity.InvestorsActivity;
import lanou.a36ke_01.activity.discoveryactivity.PressActivity;
import lanou.a36ke_01.activity.equityactivity.EquityDetailsActivity;
import lanou.a36ke_01.adapters.discoveryadapter.DiscoveryVpAdapter;
import lanou.a36ke_01.beanlists.equitybean.EquityAllBean;
import lanou.a36ke_01.beanlists.publicbean.RollBean;
import lanou.a36ke_01.fragmengs.equityfragments.EquityAllFragment;
import lanou.a36ke_01.tools.BaseFragment;
import lanou.a36ke_01.tools.MyApp;
import lanou.a36ke_01.tools.NetTool;

public class DiscoveryFragment extends BaseFragment implements View.OnClickListener {
    private ArrayList<String> list;
    private ViewPager viewPager;
    private DiscoveryVpAdapter vpAdapter;
    private LinearLayout company;
    private LinearLayout academy;
    private LinearLayout activity;
    private TextView viewAll;
    private LinearLayout findPeople , findItems ;
    private LinearLayout inverstors;
    private LinearLayout entre;
    private ImageView repair;
    private ImageView concert;
    private ImageView huatu;
    private TextView titleOne;
    private TextView titleTwo;
    private TextView titleThree;
    private TextView contextOne;
    private TextView contextTwo;
    private TextView contextThree;
    private String url = "https://rong.36kr.com/api/mobi/cf/actions/list?page=1&type=all&pageSize=20";
    private RelativeLayout itemOne;
    private RelativeLayout itemTwo;
    private RelativeLayout itemThree;
    private EquityAllBean equityAllBean;
    private TextView trade;
    private TextView project;
    private TextView investing;


    @Override
    protected int setLayout() {
        return R.layout.fragment_discovery;
    }

    @Override
    protected void initView() {
        itemOne = bindView(R.id.fragment_discovery_item_one);
        itemTwo = bindView(R.id.fragment_discovery_item_two);
        itemThree = bindView(R.id.fragment_discovery_item_three);
        repair = bindView(R.id.iv_discover_repair);
        concert = bindView(R.id.iv_discover_vocal_concert);
        huatu = bindView(R.id.iv_discover_huatuo);
        titleOne = bindView(R.id.fragment_discovery_small_title);
        titleTwo = bindView(R.id.fragment_discovery_small_title_two);
        titleThree = bindView(R.id.fragment_discovery_small_title_three);
        contextOne = bindView(R.id.fragment_discovery_small_context);
        contextTwo = bindView(R.id.fragment_discovery_small_context_two);
        contextThree = bindView(R.id.fragment_discovery_small_context_three);
        findItems = bindView(R.id.fragment_discovery_find_items);
        findPeople = bindView(R.id.fragment_discovery_find_people);
        viewAll = bindView(R.id.fragment_discovery_view_all);
        company = bindView(R.id.fragment_discovery_company);
        academy = bindView(R.id.fragment_discovery_academy);
        activity = bindView(R.id.fragment_discovery_activity);
        viewPager = bindView(R.id.fragment_discovery_viewpager);
        vpAdapter = new DiscoveryVpAdapter(MyApp.getContext());
        entre = bindView(R.id.fragment_discovery_entrepreneurs);
        inverstors = bindView(R.id.fragment_discovery_investors);
        trade = bindView(R.id.fragment_discovery_trade_news);
        project = bindView(R.id.fragment_discovery_project_news);
        investing = bindView(R.id.fragment_discovery_investing);
    }

    @Override
    protected void initData() {
        initJson();
        inverstors.setOnClickListener(this);
        entre.setOnClickListener(this);
        company.setOnClickListener(this);
        academy.setOnClickListener(this);
        activity.setOnClickListener(this);
        viewAll.setOnClickListener(this);
        findPeople.setOnClickListener(this);
        findItems.setOnClickListener(this);
        itemOne.setOnClickListener(this);
        itemTwo.setOnClickListener(this);
        itemThree.setOnClickListener(this);
        titleThree.setOnClickListener(this);
        trade.setOnClickListener(this);
        project.setOnClickListener(this);
        investing.setOnClickListener(this);

        //获取数据
        final NetTool netTool = new NetTool();
        netTool.getNetData(url, EquityAllBean.class, new NetTool.NetListener<EquityAllBean>() {
            @Override
            public void onSuccess(EquityAllBean equityAllBean) {
                netTool.getImage(equityAllBean.getData().getData().get(0).getCompany_logo(),repair);
                netTool.getImage(equityAllBean.getData().getData().get(1).getCompany_logo(),concert);
                netTool.getImage(equityAllBean.getData().getData().get(2).getCompany_logo(),huatu);
                titleOne.setText(equityAllBean.getData().getData().get(0).getCompany_name());
                titleTwo.setText(equityAllBean.getData().getData().get(1).getCompany_name());
                titleThree.setText(equityAllBean.getData().getData().get(2).getCompany_name());
                contextOne.setText(equityAllBean.getData().getData().get(0).getCompany_brief());
                contextTwo.setText(equityAllBean.getData().getData().get(1).getCompany_brief());
                contextThree.setText(equityAllBean.getData().getData().get(2).getCompany_brief());
            }

            @Override
            public void onError(String errorMsg) {

            }
        });


    }

    // 轮播图的线程
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            Message message = handler.obtainMessage();
            message.what = 1;
            handler.sendMessageDelayed(message, 5000);
            return false;
        }
    });


    // 生命周期 onStart 当Activity开始的时候 执行这个方法
    @Override
    public void onStart() {
        super.onStart();
        Message message = handler.obtainMessage();
        message.what = 1;
        handler.sendMessageDelayed(message, 5000);
    }

    //当划出这个页面的时候就停掉
    @Override
    public void onStop() {
        super.onStop();
        handler.removeMessages(1);
    }

    protected void initJson(){
        list = new ArrayList<>();
        String url = "https://rong.36kr.com/api/mobi/roundpics/v4";

        mNetTool.getNetData(url, RollBean.class, new NetTool.NetListener<RollBean>() {
            @Override
            public void onSuccess(RollBean rollBean) {
                for (int i = 0; i < rollBean.getData().getPics().size(); i++) {
                    list.add(rollBean.getData().getPics().get(i).getImgUrl());
                }
                vpAdapter.setArrayList(list);
                vpAdapter.setViewPager(viewPager);
                viewPager.setAdapter(vpAdapter);

                viewPager.setCurrentItem(3000);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fragment_discovery_company:
                Intent company = new Intent(getActivity(), LogonActivity.class);
                startActivity(company);
                break;
            case R.id.fragment_discovery_academy:
                Intent academy = new Intent(getActivity(), AcademyActivity.class);
                startActivity(academy);
                break;
            case R.id.fragment_discovery_activity:
                Intent activity = new Intent(getActivity(), BuyerNoticeActivity.class);
                startActivity(activity);

                break;
            case R.id.fragment_discovery_view_all:
                Intent intent = new Intent(getActivity(), EquityAllFragment.class);
                startActivity(intent);
                break;
            case R.id.fragment_discovery_find_items:
                Intent items = new Intent(getActivity(),LogonActivity.class);
                startActivity(items);
                break;
            case R.id.fragment_discovery_find_people:
                Intent find = new Intent(getActivity() , DiscoveryActivity.class);
                startActivity(find);
                break;
            case R.id.fragment_discovery_entrepreneurs:
                Intent entrepreneurs = new Intent(getActivity() , EntrePreneursActivity.class);
                startActivity(entrepreneurs);
                break;
            case R.id.fragment_discovery_investors:
                Intent investors = new Intent(getActivity() , InvestorsActivity.class);
                startActivity(investors);
                break;
            case R.id.fragment_discovery_item_one:
                Intent itemOne = new Intent(getActivity(), EquityDetailsActivity.class);
                itemOne.putExtra("ID","190");
                startActivity(itemOne);
                break;
            case R.id.fragment_discovery_item_two:
                Intent itemTwo = new Intent(getActivity(), EquityDetailsActivity.class);
                itemTwo.putExtra("ID","188");
                startActivity(itemTwo);
                break;
            case R.id.fragment_discovery_item_three:
                Intent itemThree = new Intent(getActivity(),EquityDetailsActivity.class);
                itemThree.putExtra("ID","180");
                startActivity(itemThree);
                break;
            case R.id.fragment_discovery_trade_news:
                Intent trade = new Intent(getActivity(), PressActivity.class);
                trade.putExtra("Id","https://z.36kr.com/news/list?type=4");
                startActivity(trade);
                break;
            case R.id.fragment_discovery_project_news:
                Intent project = new Intent(getActivity(),PressActivity.class);
                project.putExtra("Id","https://z.36kr.com/news/list?type=5");
                startActivity(project);
                break;
            case R.id.fragment_discovery_investing:
                Intent invseting = new Intent(getActivity(),PressActivity.class);
                invseting.putExtra("Id","https://z.36kr.com/news/list?type=6");
                startActivity(invseting);
                break;

        }
    }
}
